import rxAjax from '@/assets/js/ajax.js';
import FormUtil from "@/assets/js/FormUtil.js"
import {GetAjax} from '@lowcode/jpaas-base-lib';
var config={timeout:300000};
var newRxAjax=GetAjax(config);

export const SysAppApi = {};

SysAppApi.baseUrl= '/api-system/system/core/sysApp';
SysAppApi.appBaseUrl = '/api-system/system/core/sysAppFavorites';

SysAppApi.query=function (parameter) {
  var url= SysAppApi.baseUrl + '/query';
  return rxAjax.postJson(url,parameter).then (res => {
    return res.result
  })
}

SysAppApi.getAllApps=function(parameter) {
  let url=SysAppApi.baseUrl + '/getAllApps';
  return rxAjax.get(url);
}

SysAppApi.getAllSysAppList=function (parameter) {
  var url= SysAppApi.baseUrl + '/getAll';
  return rxAjax.get(url);
}
//通过应用ID查找应用，如果应用ID为空则显示系统内置的应用
SysAppApi.getById=function (appId) {
    var url= SysAppApi.baseUrl + '/getById?appId=' + appId;
    return rxAjax.get(url);
}
//获取非单页面应用
SysAppApi.getApps=function (appId) {
  var url= SysAppApi.baseUrl + '/getApps?appId=' + appId;
  return rxAjax.get(url);
}

//获取所有用户开发应用
SysAppApi.getAllUserAppsByAppName=function (appName) {
    var url= SysAppApi.baseUrl + '/getAllUserAppsByAppName?appName=' + appName;
    return rxAjax.get(url);
}

/**
 * 获取单记录
 * @param pkId
 * @returns {*}
 */
SysAppApi.get =function(pkId) {
  var url= SysAppApi.baseUrl + '/get?pkId=' + pkId;
  return rxAjax.get(url);
}

SysAppApi.save =function(parameter) {
  var url= SysAppApi.baseUrl + '/save';
  return rxAjax.postJson(url,parameter);
}

SysAppApi.del =function(parameter) {
  var url= SysAppApi.baseUrl + '/delById';
  return newRxAjax.postUrl(url,parameter);
}
SysAppApi.delByIds =function(parameter) {
    var url= SysAppApi.baseUrl + '/del';
    return rxAjax.postUrl(url,parameter);
}
//导入应用
SysAppApi.doImport=function(formData) {
    var url= SysAppApi.baseUrl+"/doImport";
    // return rxAjax.postJson(url,data);
    return newRxAjax.upload(url,formData);
}
//导入应用校验
SysAppApi.importCheck=function(formData,callback) {
    var url= SysAppApi.baseUrl+"/importCheck";
    return newRxAjax.upload(url,formData,callback);
}


/**
 * 获取当前用户最近使用的应用
 */
SysAppApi.getLastUseApps =function() {
    var url=  `${SysAppApi.appBaseUrl}/getLastUseApps`;
    return rxAjax.get(url);
}

/**
 * 保存最近使用应用
 * 参数为：appId-应用ID, isAdd-是否增加（1增加，其他为移除）
 */
SysAppApi.saveLastUse =function(parameter) {
    var url= `${SysAppApi.appBaseUrl}/saveLastUse`;
    return rxAjax.postUrl(url,parameter);
}

/**
 * 获取当前用户收藏的应用
 */
SysAppApi.getMyApps =function() {
    var url= `${SysAppApi.appBaseUrl}/getMyApps`;
    return rxAjax.get(url);
}
/**
 * 添加收藏，参数为：appId
 */
SysAppApi.saveFavorite =function(parameter) {
    let url= `${SysAppApi.appBaseUrl}/saveFavorite`;
    return rxAjax.postUrl(url,parameter);
}
// 清空当前用户的最近使用
SysAppApi.cancelRecentlyUsed =function(parameter) {
    let url= `${SysAppApi.appBaseUrl}/cancelRecentlyUsed`;
    return rxAjax.postUrl(url,parameter);
}
// 根据ID删除最近使用
SysAppApi.cancelRecentlyUsedById =function(ids,parameter) {
    let url= `${SysAppApi.appBaseUrl}/del?ids=${ids}`;
    return rxAjax.postUrl(url,parameter);
}

//导出应用
SysAppApi.doExport= function (params){
    var url= SysAppApi.baseUrl + '/doExport' ;
    FormUtil.doExport(url,params);
}
//复制应用
SysAppApi.copy= function(params){
    var url= SysAppApi.baseUrl + '/copy' ;
    return rxAjax.postJson(url,params);
}
//按分类和关键字检索二次开发的应用
SysAppApi.getByCategoryOrKey =function(params) {
    var url= SysAppApi.baseUrl + '/getByCategoryOrKey';
    return rxAjax.postUrl(url,params);
}
//获取当前用户有权限的应用
SysAppApi.getMyAuthApp =function() {
    var url= SysAppApi.baseUrl + '/getMyAuthApp';
    return rxAjax.get(url);
}
//获取各分类应用数量
SysAppApi.getCountByCategory =function() {
    var url= SysAppApi.baseUrl + '/getCountByCategory';
    return rxAjax.postUrl(url,null);
}
// 获取应用市场数据
SysAppApi.getAppMarketData =function() {
    let url = SysAppApi.baseUrl + '/market/getAppMarketData';
    return rxAjax.postUrl(url,null);
}
// 安装应用
SysAppApi.installApp = function(params) {
    let url = SysAppApi.baseUrl + '/market/install';
    return rxAjax.postJson(url, params);
}
// 获取我的应用
SysAppApi.getMyApp = function(params) {
    let url = SysAppApi.baseUrl + '/market/getMyApp';
    return rxAjax.postJson(url, params);
}
// 暂停或启用
SysAppApi.startOrStop = function(params) {
    let url = SysAppApi.baseUrl + '/market/startOrStop';
    return rxAjax.postJson(url, params);
}
// 卸载
SysAppApi.uninstall = function(params) {
    let url = SysAppApi.baseUrl + '/market/uninstall';
    return rxAjax.postJson(url, params);
}
// 获取机构列表数据
SysAppApi.getInstData = function() {
    let url = SysAppApi.baseUrl + '/market/getInstData';
    return rxAjax.postUrl(url,null);
}
// 获取应用安装数据
SysAppApi.getAppInstallData = function() {
    let url = SysAppApi.baseUrl + '/market/getAppInstallData';
    return rxAjax.postUrl(url,null);
}
// 删除安装数据
SysAppApi.deleteByTenantIds = function(params) {
    let url = SysAppApi.baseUrl + '/market/deleteByTenantIds';
    return rxAjax.postJson(url, params);
}

SysAppApi.getCompanyAddGrantApps =function() {
    var url= SysAppApi.baseUrl + '/getCompanyAddGrantApps';
    return rxAjax.get(url);
}

SysAppApi.getGrantList =function() {
    var url= SysAppApi.baseUrl + '/getGrantList';
    return rxAjax.get(url);
}

SysAppApi.downloadFront=function() {
    var url= SysAppApi.baseUrl + '/downloadFront' ;
    FormUtil.doExport(url,"front");
}

SysAppApi.downloadBackend=function(ent) {
    var url= SysAppApi.baseUrl + '/downloadBackend' ;
    FormUtil.doExport(url,ent);
}
SysAppApi.backupTable =function(params) {
    var url= SysAppApi.baseUrl + '/backupTable';
    return rxAjax.postJson(url,params);
}
//导出非应用数据
SysAppApi.dataExport= function (params){
    var url= SysAppApi.baseUrl + '/dataExport' ;
    return FormUtil.doExport(url,params);
}
// 数据还原
SysAppApi.dataRevert= function (params){
    var url= '/api-system/system/core/sysDataMove/dataRevert' ;
    return rxAjax.postUrl(url,params);
}
// 获取数据导出配置
SysAppApi.getExportConfig= function (params){
    var url= '/api-system/system/core/sysDataMove/getExportConfig' ;
    return rxAjax.postUrl(url,params);
}

// 获取拼音
SysAppApi.getPinyin =function(chinese) {
    var url= "/api-form/form/share/pinyin/getPinyin";
    if(!chinese){
        return ;
    }
    var params={chinese:chinese,lower:true};
    return rxAjax.postForm(url,params);
}

// 获取数据导出导入备份数据
SysAppApi.getOverrideData= function (fileId){
    var url= '/api-system/system/core/sysDataMove/getOverrideData?fileId='+fileId;
    return rxAjax.get(url);
}

//按分类和关键字检索二次开发的应用
SysAppApi.getMyOwnApp =function(params) {
    var url= SysAppApi.baseUrl + '/getMyOwnApp';
    return rxAjax.postJson(url,params);
}

//获取我的代码所属应用
SysAppApi.getMyOwnBpmApp =function(params) {
    var url= SysAppApi.baseUrl + '/getMyOwnBpmApp';
    return rxAjax.postJson(url,params);
}

/**
 * 根据应用别名获取应用对象.
 * @param appKey
 * @returns {*}
 */
SysAppApi.getByAppKey =function(appKey) {
    var url= SysAppApi.baseUrl + '/getByAppKey?appKey=' +appKey;
    return rxAjax.get(url);
}


export  default SysAppApi;
